package com.shenyue.nbtownlet.activity;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.base.BaseActivity;
import com.shenyue.nbtownlet.databinding.ActivityProjectDetailBinding;
import com.shenyue.nbtownlet.event.BaseListener;
import com.shenyue.nbtownlet.model.DefaultModel;
import com.shenyue.nbtownlet.model.AppProjectModel;
import com.shenyue.nbtownlet.util.StringUtil;
import com.trycatch.mysnackbar.Prompt;
import com.trycatch.mysnackbar.TSnackbar;

/**
 * 项目详情
 */

public class ProjectDetailActivity extends BaseActivity implements BaseListener, View.OnClickListener {
    private ActivityProjectDetailBinding binding;
    private Context mContext;
    private Handler mHandler = new Handler();
    private String ID;
    private TSnackbar snackbar;
    private JSONObject $data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        ID = bundle.getString(DefaultModel.PAGE_DATA_ID);
        String name = bundle.getString(DefaultModel.PAGE_NAME);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_project_detail);
        setActionBar(true, name);

        mContext = this;

        snackbar = showTSnackbar("正在获取详情..., 请稍候");

        getProjectDetails();
    }

    private void getProjectDetails() {
        JSONObject param = new JSONObject();
        param.put("Projectid", ID);
        AppProjectModel.GetProjectDetails(mContext, param, this);
    }

    @Override
    public void success(String val, String action) {
        hideTSnackbar(snackbar, "获取信息成功");
        if (AppProjectModel.GETPROJECTDETAILS_ACTION.equals(action)) {
            $data = JSON.parseObject(val);
            binding.NAME.setText(StringUtil.getValue($data.getString("NAME")));
            binding.AREANAMESTREETNAME.setText(StringUtil.getValue($data.getString("AREANAME")) + "\u3000" + StringUtil.getValue($data.getString("STREETNAME")));
            binding.TYPENAME.setText(StringUtil.getValue($data.getString("TYPENAME")));
            binding.CONTACTNAME.setText(StringUtil.getValue($data.getString("CONTACTNAME")));
            binding.CONTACTTEL.setText(StringUtil.getValue($data.getString("CONTACTTEL")));
            binding.PROGRESS.setText(StringUtil.getValue($data.getString("PROGRESS")));
            String ENDTIME = StringUtil.getValue($data.getString("ENDTIME"));
            if(ENDTIME.contains("T"))
                ENDTIME = ENDTIME.substring(0, ENDTIME.indexOf("T"));
            binding.ENDTIME.setText(ENDTIME);
            binding.DESCRIBE.setText("\u3000\u3000" + StringUtil.getValue($data.getString("DESCRIBE")));
            binding.PAY2.setOnClickListener(this);
            binding.SOURCES2.setOnClickListener(this);
        }
    }

    @Override
    public void error(String error, String action) {
        hideTSnackbar(snackbar, error, Prompt.WARNING);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.PAY2: {//资金拨付列表
                Bundle bundle = new Bundle();
                bundle.putString(DefaultModel.PAGE_NAME, "资金拨付列表");
                bundle.putString(DefaultModel.PAGE_DATA, $data.getString("PAY2"));
                toActivity(ProjectFundActivity.class, bundle);
                break;
            }
            case R.id.SOURCES2: {//资金使用列表
                Bundle bundle = new Bundle();
                bundle.putString(DefaultModel.PAGE_NAME, "资金使用列表");
                bundle.putString(DefaultModel.PAGE_DATA, $data.getString("SOURCES2"));
                toActivity(ProjectFundActivity.class, bundle);
                break;
            }
        }
    }
}
